package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.mapper.*;
import com.heima.pojo.*;
import com.heima.pojo.dto.RegionPageQueryDTO;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.VmVo;
import com.heima.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(RegionPageQueryDTO dto) {
        //分页参数
        Page<Node> page = Page.of(dto.getPageIndex(), dto.getPageSize());
        //准备查询条件
        LambdaQueryWrapper<Node> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getRegionId() != null, Node::getRegionId, dto.getRegionId())
                .orderByDesc(Node::getUpdateTime);

        //拷贝主要参数
        Page<Node> nodePage = nodeMapper.selectPage(page, wrapper);
        List<Node> nodes = nodePage.getRecords();
        ArrayList<NodeVo> nodeVos = new ArrayList<>();
        nodes.forEach(node -> {
            NodeVo nodeVo = new NodeVo();
            BeanUtils.copyProperties(node, nodeVo);
            nodeVos.add(nodeVo);
        });

        //设置次要参数
        nodeVos.forEach(nodeVo -> {
            LambdaQueryWrapper<Vm> vmWrapper = new LambdaQueryWrapper<>();
            vmWrapper.eq(Vm::getOwnerId, nodeVo.getOwnerId());
            nodeVo.setVmCount(Integer.parseInt(vmMapper.selectCount(vmWrapper).toString()));
            nodeVo.setRegion(regionMapper.selectById(nodeVo.getRegionId()));
            nodeVo.setBusinessType(businessMapper.selectById(nodeVo.getBusinessId()));
        });

        return new PageResult(dto.getPageIndex(), dto.getPageSize(), nodePage.getTotal(), nodeVos);
    }

    /**
     * 新增点位
     *
     * @param node
     */
    @Override
    public void insert(Node node) {
        nodeMapper.insert(node);
    }

    /**
     * 查询点位详情
     *
     * @param nodeId
     * @return
     */
    @Override
    public List<VmVo> queryNodeDetails(Long nodeId) {
        //根据nodeId查询vm的集合
        LambdaQueryWrapper<Vm> vmWrapper = new LambdaQueryWrapper<>();
        vmWrapper.eq(Vm::getNodeId, nodeId);
        List<Vm> vms = vmMapper.selectList(vmWrapper);
        List<VmVo> vmVos = vms.stream().map(vm -> {
            VmVo vmVo = new VmVo();
            BeanUtils.copyProperties(vm, vmVo);
            return vmVo;
        }).toList();
        //在其他表中查询其他数据
        Node node = nodeMapper.selectById(nodeId);
        Region region = regionMapper.selectById(node.getRegionId());

        //处理nodes转nodeVo
        NodeVo nodeVo = new NodeVo();
        BeanUtils.copyProperties(node, nodeVo);

        //处理regions转regionsVo
        RegionVo regionVo = new RegionVo();
        BeanUtils.copyProperties(region, regionVo);


        LambdaQueryWrapper<Node> nodeWrapper = new LambdaQueryWrapper<>();
        nodeWrapper.eq(Node::getRegionId, regionVo.getId());
        regionVo.setNodeCount(Integer.parseInt(nodeMapper.selectCount(nodeWrapper).toString()));
        regionVo.setNodeList(nodeMapper.selectList(nodeWrapper));


        //根据nodeVos得到businessids
        Business business = businessMapper.selectById(nodeVo.getBusinessId());
        nodeVo.setBusinessType(business);

        for (int i = 0; i < vmVos.size(); i++) {
            Integer vmType = vmVos.get(i).getVmType();
            VmType type = vmTypeMapper.selectById(vmType);
            vmVos.get(i).setType(type);
            vmVos.get(i).setNode(nodeVo);
            vmVos.get(i).setRegion(regionVo);
        }

        return vmVos;
    }

    @Override
    public void update(Long nodeId, Node node) {
        node.setId(nodeId);
        nodeMapper.updateById(node);
    }
}
